package com.sanley.piss.web.controller;


import com.sanley.piss.mapper.FamilyMapper;
import com.sanley.piss.model.exceptionUtil.json.JsonResult;
import com.sanley.piss.model.po.Family;
import com.sanley.piss.model.vo.FamilyVO;
import com.sanley.piss.web.service.mapper.IFamilyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Create By  on 2021/3/13.
 *
 * @author 张凯大天才
 */
@Api(value = "家庭表controller", tags = "家庭表操作接口")
@RestController
@RequestMapping("/family")
public class FamilyController {
    @Autowired
    private IFamilyService service;

    @Autowired
    private FamilyMapper mapper;

    @ApiImplicitParam(name = "userId", value = "对应用户基本信息ID", paramType = "path", required = true)
    @ApiOperation(value = "根据基本信息表ID插入一条记录")
    @RequestMapping(value = "/{userId}/insert", method = RequestMethod.PUT)
    public JsonResult<Object> insert(@Valid FamilyVO family, @PathVariable("userId") Integer userId) {
        service.insert(family, userId);
        return JsonResult.success();
    }

    @ApiOperation(value = "根据ID删除多条数据")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.DELETE)
    public JsonResult<Object> deleteById(@PathVariable("ids") Integer[] ids) {
        service.removeByIds(ids);
        return JsonResult.success();
    }

    @ApiOperation(value = "根据ID修改一条记录")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResult<Object> updateById(@Valid FamilyVO family) {
        service.updateById(family);
        return JsonResult.success();
    }

    @ApiOperation(value = "查询该用户注册的所有记录")
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public List<Family> selectAll() {
        return mapper.selectAll();
    }

    @ApiOperation(value = "查询该用户注册的一条ID记录")
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    public JsonResult<FamilyVO> selectById(Integer id) {
        return JsonResult.success(service.selectById(id));
    }


}
